package com.sl.au.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.sl.au.entity.Hierarchy;
import com.sl.au.entity.HierarchyForMailList;
import com.sl.au.entity.User;
import com.sl.au.service.HierarchyService;
import com.sl.au.service.UserService;

/**
 * 通讯录
 * @author yangdi
 *
 */
@RestController
@RequestMapping(value = "/ui/mailList") 
public class MailListController  extends BaseController{

	@Autowired
	private UserService userService;
	@Autowired
	private HierarchyService hierarchyService;
	
	
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index(ModelAndView modelAndView,HttpServletRequest request) {
		String hierarchy = request.getParameter("hierarchy");
		String back = "";
		String device = request.getParameter("device");
		String identify = request.getParameter("identify");
		modelAndView.setViewName("mobile/mailList/mailListIndex");
		modelAndView.addObject("identify", identify);
		if(!StringUtils.hasText(hierarchy) || hierarchy.equals("0")) {
			hierarchy = "0";
			back = "";
		}else {
			Hierarchy hierarchyInfo = hierarchyService.getRepository().findOne(hierarchy);
			back = hierarchyInfo.getGroupParent();
		}
		modelAndView.addObject("hierarchy", hierarchy);
		modelAndView.addObject("back", back);
		return modelAndView;
	}
	
	@RequestMapping("/findHierarchys/{id}")
	public Map<String,Object> findHierarchys(@PathVariable("id") String id) {
		Map<String,Object> map = new HashMap<String,Object>();
		//查询组织信息
//		List<Hierarchy> hierarchys = hierarchyService.getRepository().findByParentId(id);
		List<HierarchyForMailList> hierarchys = hierarchyService.findHierarchyForMailList(id);
		//查询人员信息
		List	<User> users = userService.getRepository().getUsersByHierarchy(id);
		
		map.put("hierarchys", hierarchys);
		map.put("users", users);
		return map;
	}
	
	@RequestMapping(value = "/findSearch", method = RequestMethod.POST)
	public Map<String,Object> findSearch(String value) {
		Map<String,Object> map = new HashMap<String,Object>();
		//List<HierarchyForMailList> hierarchys = new ArrayList<HierarchyForMailList>();
		List	<User> users = new ArrayList<User>();
		if(StringUtils.hasText(value)) {
			//查询组织信息
			//hierarchys = hierarchyService.findHierarchyForMailListByKey(value);
			//查询人员信息
			users = userService.getRepository().getUsersByName(value);
		}
		
		//map.put("hierarchys", hierarchys);
		map.put("users", users);
		return map;
	}
	
	@RequestMapping("/findHierarchyInfon/{id}")
	public Map<String,Object> findHierarchyInfon(@PathVariable("id") String id) {
		Map<String,Object> map = new HashMap<String,Object>();
		Hierarchy hierarchy = hierarchyService.getRepository().findOne(id);
		map.put("hierarchy", hierarchy);
		return map;
	}

	@RequestMapping("/findUserInfon/{id}")
	public Map<String,Object> findUserInfon(@PathVariable("id") String id) {
		Map<String,Object> map = new HashMap<String,Object>();
		User user = userService.getRepository().findOne(id);
		map.put("user", user);
		return map;
	}
	
}
